package com.example.shushiyunsst.fragment.main.home.child.miner22.mother_icon

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.fastjson.JSON
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.base.LazyFragment
import com.example.library_common.networking.RestClient
import com.example.library_common.utils.even.MessageWrap
import com.example.library_common.utils.even.TO_REFRESH_LIST
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.constant.CURRENT_DEVICE_TOKEN
import com.example.shushiyunsst.fragment.main.home.child.bean.MotherIconBean
import com.example.shushiyunsst.util.MOTHER_ICON
import com.example.wallet.db.UserInfoManager
import com.example.wallet.utils.Preference

/**
 * 母币
 * */
@Route(path = MOTHER_ICON)
class MotherIconFragment : LazyFragment(), MotherIconInterfaces {

    companion object {
        fun newInstance(): MotherIconFragment {
            val args = Bundle()
            val fragment = MotherIconFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private val mHeaderFragment = MotherIconInfoHeaderFragment.newInstance()

    private val mRecorderListFragment = MotherIconRecorderListFragment.newInstance()

    private var mRefresh:View?=null

    override fun getLayoutId(): Any {
        return R.layout.mother_icon_fragment
    }

    override fun lazyInit() {

        mHeaderFragment.setMotherIconInterfaces(this)
        loadRootFragment(R.id.motherIconInfoHeader, mHeaderFragment)
        loadRootFragment(R.id.motherIconRecorderList, mRecorderListFragment)
        rootView.postDelayed({
            onRefresh()
        },500)

    }

    override fun handMessage(msg: MessageWrap?) {
//        super.handMessage(msg)
        when (msg?.message) {
            TO_REFRESH_LIST -> {
                onRefresh()
            }
        }
    }

    override fun initView(view: View) {
        mRefresh=view.findViewById(R.id.mRefresh)
        view.findViewById<View>(R.id.mPageBack).setOnClickListener { onBackPressedSupport() }
    }

    override fun onBackPressedSupport(): Boolean {
        pop()
        return true
    }

    override fun onRefresh() {
        showRefresh()
        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo
        val deviceTokens = Preference.HOLDER.getCustomAppProfile(CURRENT_DEVICE_TOKEN)
        RestClient.builder()
            .url(R.string.scmlAccount)
            .raw(
                JSON.toJSONString(
                    mapOf(
                        "uid" to userInfo?.uid,
                        "deviceToken" to deviceTokens
                    )
                )
            )
            .success {
                Log.d("netQuest", "netQuest: $it")
                val obj = JSON.parseObject(it, MotherIconBean::class.java)
                mHeaderFragment.setEntity(obj)
                mRecorderListFragment.setBean(obj)
                mRefresh?.visibility=View.GONE
            }
            .failure {
                Toast.makeText(_mActivity, "获取母币数据失败", Toast.LENGTH_SHORT).show()
                mRefresh?.visibility=View.GONE
            }
            .error { _, msg ->
                Toast.makeText(_mActivity, msg, Toast.LENGTH_SHORT).show()
                mRefresh?.visibility=View.GONE
            }
            .build().post()
    }

    override fun showRefresh() {
        mRefresh?.visibility=View.VISIBLE
    }
}